package com.version1_2.controller;

import com.version1_1.controller.SubscriController;
import com.version1_2.service.SubscriService1_2;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * Created by reeves on 2015/8/23.
 */

@Controller
public class SubscriController1_2 {

    /*创建log4j对象*/
    private static final Logger LOGGER = Logger.getLogger(SubscriController1_2.class);

    @Autowired
    private SubscriController subscriController;

    @Autowired
    private SubscriService1_2 subscriService12;

    /**
     * @Description: 获取推荐界面信息 OK
     */
    @RequestMapping(value = "/version1_2/recommend/getRecommendPage", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getNewSubscriPage(HttpServletRequest request) throws UnsupportedEncodingException {

        boolean cleanCache = false;
        if (request.getParameter("cleanCache") != null) {
            cleanCache = Boolean.parseBoolean((String) request.getParameter("cleanCache"));
        }

        return subscriService12.getNewSubscriPage(cleanCache);
    }

    /*------------------------------------------ 复用version1.1 SubscriController接口 ------------------------------------------*/

    /**
     * @Description: 获取热门分类
     */
    @RequestMapping(value = "/version1_2/subscri/getHotSortation/{number}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getHotSortation(@PathVariable("number") int number) throws UnsupportedEncodingException {
        return subscriController.getHotSortation(number);
    }

    /**
     * @Description: 订阅单独的三级分类 OK
     */
    @RequestMapping(value = "/version1_2/subscri/subscriThirdType/{userId}/{secondType}/{thirdTypeName}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String subscriThirdType(@PathVariable("userId") int userId, @PathVariable("secondType") int secondType, @PathVariable("thirdTypeName") String thirdTypeName) {
        return subscriController.subscriThirdType(userId, secondType, thirdTypeName);
    }


    /**
     * @Description: 取消订阅单独的三级分类 OK
     */
    @RequestMapping(value = "/version1_2/subscri/removeSubscri/{userId}/{secondType}/{thirdTypeName}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String removeSubscri(@PathVariable("userId") int userId, @PathVariable("secondType") int secondType, @PathVariable("thirdTypeName") String thirdTypeName) {

        /**
         * {
         *     "removeSubscriResult":true/false
         * }
         */
        return subscriController.removeSubscri(userId, secondType, thirdTypeName);
    }


    /**
     * @Description: 更改用户订阅排序
     */
/*    @RequestMapping(value = "/subscri/changeSubscriOrder/{userId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String changeSubscriOrder(@PathVariable("userId") int userId, HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");

        *//**
     *  updateUserSubscriOrderStr
     *  {
     *    [
     *      {
     *        "firstType":,
     *        "secondTypeOrder":[ //排序没动就发[]
     *          {
     *            "secondType":,
     *            "secondTypeOrder":
     *          }],
     *        "thirdTypeOrder":[ //排序没动就发[]
     *          {
     *            "secondType":,
     *            "thirdNameList":[...]
     *          }...
     *          ]
     *      }...
     *    ]
     *  }
     *//*
        String updateUserSubscriOrderStr = request.getParameter("updateUserSubscriOrderStr");
        boolean result = false;
        Map<String, Object> changeSubscriOrderMap = new HashMap<>();

        // 服务器未接收到客户端参数
        if (updateUserSubscriOrderStr == null) {
            LOGGER.error("更改用户订阅排序,未接收到参数");
        } else {
            result = subscriService.updateUserSubscriOrder(userId, updateUserSubscriOrderStr);
        }
        return new MapToJson("updateUserSubscriOrderResult", result).toJsonString();
    }*/

}
